#include "CtrlRadarChart.h"
#include <QLineSeries>
#include <QValueAxis>
#include <QXYSeries>
#include <qabstractaxis.h>
#include <qchartview.h>
#include <qscatterseries.h>

CtrlRadarChart::CtrlRadarChart(QWidget *parent) : QChartView(parent) {
  isDragging = false;
  setRenderHint(QPainter::Antialiasing);
}

void CtrlRadarChart::mousePressEvent(QMouseEvent *event) {
  if (event->button() == Qt::LeftButton) {
    lastPos = event->pos();
    isDragging = true;
  }
  QChartView::mousePressEvent(event);
}

void CtrlRadarChart::mouseMoveEvent(QMouseEvent *event) {
  if (isDragging) {
    QPointF delta = mapToScene(event->pos()) - mapToScene(lastPos);
    chart()->scroll(-delta.x(), delta.y());

    lastPos = event->pos();
  }
  QChartView::mouseMoveEvent(event);
}

void CtrlRadarChart::mouseReleaseEvent(QMouseEvent *event) {
  if (event->button() == Qt::LeftButton) {
    isDragging = false;
  }
  QChartView::mouseReleaseEvent(event);
}

void CtrlRadarChart::mouseDoubleClickEvent(QMouseEvent *event) {
  QPointF dpoint = chart()->mapToValue(event->pos());
  emit SPointClicked(dpoint);

  QChartView::mouseDoubleClickEvent(event); // 继续处理双击事件
}

void CtrlRadarChart::wheelEvent(QWheelEvent *event) {
  // 获取缩放因子
  qreal factor = event->angleDelta().y() > 0 ? 1.1 : 0.9;

  // 只缩放 X 轴
  QList<QAbstractAxis *> allAxes = chart()->axes(Qt::Horizontal);
  for (QAbstractAxis *axis : allAxes) {
    QValueAxis *valueAxis = qobject_cast<QValueAxis *>(axis);
    qreal range = valueAxis->max() - valueAxis->min();
    qreal center = (valueAxis->min() + valueAxis->max()) / 2.0;
    qreal newRange = range / factor;
    valueAxis->setRange(center - newRange / 2.0, center + newRange / 2.0);
  }
  // 调用基类的处理方法
  QChartView::wheelEvent(event);
}

void CtrlRadarChart::UpdateRadarData(QVector<uint32_t> data) {
  uint32_t idx = 0;
  QChart *chart = this->chart();
  chart->removeAllSeries();
  QXYSeries *series = nullptr;
  series = new QLineSeries();
  series->setName("雷达数据");
  QVector<uint32_t>::iterator it = data.begin();
  for (; it != data.end(); it++) {
    series->append(idx, *it);
    idx++;
  }
  chart->addSeries(series);
  chart->createDefaultAxes();
  this->setRenderHint(QPainter::Antialiasing);
}

void CtrlRadarChart::MarkPoint(QString name, QVector<QPoint> &mark_point,
                               Qt::GlobalColor color, int size, int shape) {
  QList<QAbstractAxis *> axes = this->chart()->axes();
  QValueAxis *xAxis = nullptr;
  QValueAxis *yAxis = nullptr;
  for (QAbstractAxis *axis : axes) {
    if (axis->orientation() == Qt::Horizontal) {
      xAxis = dynamic_cast<QValueAxis *>(axis);
    } else if (axis->orientation() == Qt::Vertical) {
      yAxis = dynamic_cast<QValueAxis *>(axis);
    }
  }
  QScatterSeries *scatter = new QScatterSeries();
  QChart *chart = this->chart();
  scatter->setName(name);
  scatter->setMarkerSize(size);
  scatter->setMarkerShape((QScatterSeries::MarkerShape)shape);
  scatter->setBrush(color);
  for (const auto &point : mark_point) {
    scatter->append(QPointF(point));
  }
  chart->addSeries(scatter);
  if(xAxis){
    scatter->attachAxis(xAxis);
  }
  if(yAxis) {
    scatter->attachAxis(yAxis);
  }
}